const path = require("path");
const os = require("os");
const config = require("./config")
const logPath = path.resolve(os.homedir(), config.getAppDataDir(), "runjava")

const winston = require('winston');
require('winston-daily-rotate-file');
const loggerName = "runjava"

const customFormat = winston.format.printf((info) => {
  return `[${info.timestamp}] [${info.level}] ${info.label} - ${info.message}`
})

const transport = new winston.transports.DailyRotateFile({
  dirname: logPath,
  filename: 'log-%DATE%.log',
  datePattern: 'YYYY-MM-DD',
  zippedArchive: true,
  maxSize: '100m',
  maxFiles: '3d'
});

transport.on('rotate', function (oldFilename, newFilename) {
  // do something fun
});

const logger = winston.createLogger({
  level: "silly",
  format: winston.format.combine(
    winston.format.label({label: loggerName}),
    winston.format.simple(),
    winston.format.timestamp({format: "YYYY-MM-DD HH:mm:ss.SSS"}),
    winston.format.prettyPrint(),
    customFormat,
  ),
  transports: [transport],
});

if (process.env.NODE_ENV !== 'production') {
  logger.add(new winston.transports.Console({
    level: 'info',
    format: winston.format.combine(
      winston.format.colorize(),
      winston.format.label({label: loggerName}),
      winston.format.simple(),
      winston.format.timestamp({format: "YYYY-MM-DD HH:mm:ss.SSS"}),
      winston.format.prettyPrint(),
      customFormat,
    )
  }));
}

module.exports = logger
